<template>
  <div class="login">
    <el-card header="请先登录" class="loginCard">
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref } from "vue";
import { logins } from "@/api";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

export default {
  setup() {
    const model = ref({});
    const router = useRouter();
    async function login() {
      const { data: res } = await logins(model.value);
      // console.log(res);
      //   保存到本地 浏览器关闭也能访问
      localStorage.token = res;
      //   保存到本地 浏览器关闭就没了
      //   sessionStorage.token = res;
      router.push("/");
      ElMessage({
        type: "success",
        message: "登录成功",
      });
    }
    return { model, login };
  },
};
</script>

<style>
.loginCard {
  width: 30rem;
  margin: 10rem auto;
}
</style>
